import typescript from "@rollup/plugin-typescript";
import { readFileSync } from "fs";
import { terser } from "rollup-plugin-terser";
import alias from "@rollup/plugin-alias";
// 读取UMD全局模块名，在package中定义了
const packageJson = JSON.parse(readFileSync("./package.json", "utf8"));
const pkgName = packageJson.umdModuleName;

const globals = {
  // 配置依赖中的UMD全局变量名
  "utils-lib-js": "UtilsLib",
};
export default {
  input: "src/index.ts",
  treeshake: true,
  output: [
    {
      file: "dist/esm/index.js",
      format: "esm",
    },
    {
      file: "dist/umd/index.js",
      format: "umd",
      name: pkgName,
      globals,
    },
    {
      file: "dist/cjs/index.js",
      format: "cjs",
    },
    {
      file: "dist/bundle/index.js",
      format: "iife",
      name: pkgName,
      plugins: [terser()],
      globals,
    },
  ],
  plugins: [
    typescript({
      tsconfig: "./tsconfig.json",
      include: ["src/**/*", "global.d.ts"],
    }),
    alias({
      resolve: [".js"],
    }),
  ],
};
